/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.Car;
import Model.Street;
import Model.Traffic;
import Model.TrafficLight;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;

/**
 *
 * @author Manpreet
 */
public class Display extends JPanel {

    private Car carX;
    private Car carY;
    private int LAjt;
    private int LWidth;
    private Street X;
    private Street Y;
    private ArrayList<Car> Xcars;
    private ArrayList<Car> Ycars;
    private boolean paused = false;
    private TrafficLight V;
    private TrafficLight H;
    private Traffic flow;
    private Timer timer;
    private int turns;
    private int tx;
    private int ty;

    /**
     * Creates simulation of traffic in a panel
     *
     */
    public Display() {

        X = new Street();
        Y = new Street();
        carX = new Car();
        carY = new Car();
        LAjt = 15;
        LWidth = 30;
        Xcars = new ArrayList();
        Ycars = new ArrayList();
        V = new TrafficLight();
        H = new TrafficLight();
        H.setLoc(300, 160);
        V.setLoc(180, 320);
        H.setColorRed();
        V.setColorGreen();
        flow = new Traffic();
        turns = 2;
        tx = 10;
        ty = 20;



        timer = new Timer();

        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                if (!paused) {
                    repaint();
                }
            }
        };

        TimerTask LightsChange = new TimerTask() {

            @Override
            public void run() {
                if (!paused) {

                     
                    turns += 2;

                    if (H.getColor() == Color.orange) {
                        H.setColorRed();
                        V.setColorGreen();

                    }
                    if (V.getColor() == Color.orange) {
                        V.setColorRed();
                        H.setColorGreen();

                    }
                                    }
            }
        };

        TimerTask trafficFlow = new TimerTask() {

            @Override
            public void run() {
                if (!paused) {

                    if (turns == ty) {
                            setLightDown();
                            ty += 20;
                        }
                        if (turns == tx) {
                            setLightAcross();
                            tx += 20;
                        }
                    if (!Xcars.isEmpty()) {
                        for (int i = 0; i < Xcars.size(); i++) {
                            if (H.getColor() == Color.red || H.getColor() == Color.orange) {

                                if (Xcars.get(i).getCarX() < H.getX()-25) {
                                    if (Xcars.get(i).getNum() == 1) {
                                        Xcars.get(i).moveX(1);
                                    }
                                    for (int j = 0; j < Xcars.size(); j++) {
                                        if (Xcars.get(i).getLane() == Xcars.get(j).getLane()
                                                && Xcars.get(i).getNum() == (Xcars.get(j).getNum() + 1)
                                                && Xcars.get(i).getCarX() < (Xcars.get(j).getCarX() - 10 - Xcars.get(i).getHeight())) {
                                            Xcars.get(i).moveX(1);
                                        }
                                    }
                                }
                                if (Xcars.get(i).getCarX() > H.getX()-25) {
                                    Xcars.get(i).moveX(1);
                                }
                            }

                            if (H.getColor() == Color.green) {
                                if (Xcars.get(i).getNum() == 1) {
                                        Xcars.get(i).moveX(1);
                                    }
                                    for (int j = 0; j < Xcars.size(); j++) {
                                        if (Xcars.get(i).getLane() == Xcars.get(j).getLane()
                                                && Xcars.get(i).getNum() == (Xcars.get(j).getNum() + 1)
                                                && Xcars.get(i).getCarX() < (Xcars.get(j).getCarX() - 10 - Xcars.get(i).getHeight())) {
                                            Xcars.get(i).moveX(1);
                                        }
                                    }
                            }
                        }
                    }

                    if (!Ycars.isEmpty()) {
                        for (int i = 0; i < Ycars.size(); i++) {
                            if (V.getColor() == Color.red || V.getColor() == Color.orange) {

                                if (Ycars.get(i).getCarY() < V.getY()-25) {
                                    if (Ycars.get(i).getNum() == 1) {
                                        Ycars.get(i).moveY(1);
                                    }
                                    for (int j = 0; j < Ycars.size(); j++) {
                                        if (Ycars.get(i).getLane() == Ycars.get(j).getLane()
                                                && Ycars.get(i).getNum() == (Ycars.get(j).getNum() + 1)
                                                && Ycars.get(i).getCarY() < (Ycars.get(j).getCarY() - 10 - Ycars.get(i).getLength())) {
                                            Ycars.get(i).moveY(1);
                                        }
                                    }
                                }
                                if (Ycars.get(i).getCarY() > V.getY()-25) {
                                    Ycars.get(i).moveY(1);
                                }
                            }
                            if (V.getColor() == Color.green) {
                                if (Ycars.get(i).getNum() == 1) {
                                        Ycars.get(i).moveY(1);
                                    }
                                    for (int j = 0; j < Ycars.size(); j++) {
                                        if (Ycars.get(i).getLane() == Ycars.get(j).getLane()
                                                && Ycars.get(i).getNum() == (Ycars.get(j).getNum() + 1)
                                                && Ycars.get(i).getCarY() < (Ycars.get(j).getCarY() - 10 - Ycars.get(i).getLength())) {
                                            Ycars.get(i).moveY(1);
                                        }
                                    }
                            }
                        }
                    }
                }
            }
        };

        TimerTask spawn = new TimerTask() {

            @Override
            public void run() {
                if (!paused) {
                    flow.run(carX, carY, X, Y, Xcars, Ycars, getHeight(), getWidth());

                }
            }
        };

        timer.schedule(task, 100, 100);
        timer.schedule(spawn, 100, 1000);
        timer.schedule(trafficFlow, 100, 10);
        timer.schedule(LightsChange, 100, 5000);
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(500, 500));


    }

    /**
     * Starts Simulation of the Program
     */
    public void start() {
        paused = false;
    }

    /**
     * Stops the Simulation of the Program
     */
    public void stop() {
        paused = true;
    }

    /**
     *  Changes the Lanes of the Class Street X to the given Integer i
     * @param i
     */
    public void setXlane(int i) {
        X.setLaneNum(i);
    }

    /**
     *  Changes the Lanes of the Class Street Y to the given Integer i
     * @param i
     */
    public void setYlane(int i) {
        Y.setLaneNum(i);
    }

    /**
     *  Changes the Color of the Horizontal Traffic light to Orange
     */
    public synchronized void setLightDown() {
        H.setColorOrange();
    }

    /**
     *  Changes the Color of the Vertical Traffic light to Orange
     */
    public synchronized void setLightAcross() {
        V.setColorOrange();  
    }

    @Override
    protected void paintComponent(Graphics t) {
        super.paintComponent(t);

        t.setColor(Color.GRAY);
        t.fillRect(getWidth() / 2 - LAjt * Y.getLaneNum(), 0, LWidth * Y.getLaneNum(), getHeight());

        t.setColor(Color.GRAY);
        t.fillRect(0, getHeight() / 2 - LAjt * X.getLaneNum(), getWidth(), LWidth * X.getLaneNum());


        V.draw(t);
        H.draw(t);

        if (!Xcars.isEmpty()) {
            for (int i = 0; i < Xcars.size(); i++) {
                Xcars.get(i).draw(t);
            }
        }

        if (!Ycars.isEmpty()) {
            for (int i = 0; i < Ycars.size(); i++) {
                Ycars.get(i).draw(t);
            }
        }


    }

    void setXprob(double xprob) {
        X.setProb(xprob);

    }

    void setYprob(double yprob) {
        Y.setProb(yprob);
    }
}
